package com.sdkj.dspservice.modules.analysis.api;

import com.sdkj.dspservice.common.base.controller.BaseController;
import com.sdkj.dspservice.common.react.AntdProResult;
import com.sdkj.dspservice.modules.analysis.entity.PageParamAnalysis;
import com.sdkj.dspservice.modules.analysis.service.PageParamAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "${project-config.apiUrl}/${project-config.version}/analysis/pageParamAnalysis")
public class PageParamAnalysisController extends BaseController {

    @Autowired
    private PageParamAnalysisService pageParamAnalysisService;

//    /**
//     * 直接显示同级11条
//     *
//     * @return
//     */
//    @RequestMapping(value = "listTotal2")
//    public AntdProResult listTotal2(PageParamAnalysis pageParamAnalysis) {
//        AntdProResult result = new AntdProResult();
//        List<PageParamAnalysis> pageParamAnalysisList = pageParamAnalysisService.listTotal2(pageParamAnalysis);
//        // 组装树形结构，以入参的url为父节点（最后一条），其他为子节点
//        List<PageParamAnalysis> resultList = new ArrayList<>();
//        if(!pageParamAnalysisList.isEmpty()){
//            List<PageParamAnalysis> children = new ArrayList<>();
//            PageParamAnalysis parent = pageParamAnalysisList.get(pageParamAnalysisList.size() - 1);
//            // 剔除parent本身
//            for(PageParamAnalysis us : pageParamAnalysisList){
//                if(!us.getUrl().equals(parent.getUrl())){
//                    children.add(us);
//                }
//            }
////            parent.setChildren(children);
//            resultList.add(parent);
//        }
//        result.success(resultList);
//        return result;
//    }

    /**
     * 获取列表
     *
     * @param pageParamAnalysis
     * @return
     */
    @RequestMapping(value = "list")
    public AntdProResult list(PageParamAnalysis pageParamAnalysis) {
        AntdProResult result = new AntdProResult();
        pageParamAnalysis.setLimitLeft((pageParamAnalysis.getCurrent() - 1) * 10);
        List<PageParamAnalysis> pageParamAnalysisList = pageParamAnalysisService.list(pageParamAnalysis);
        // 判断参数url是否含有query（即参数），含query则可以展开
        String[] urlSplit = pageParamAnalysis.getUrl().split("\\?");
        if(urlSplit.length > 1 && urlSplit[1].length() > 0){
            // 含query
            for (PageParamAnalysis statistics : pageParamAnalysisList) {
                statistics.setChildren(new ArrayList<>());
            }
        }
        result.success(pageParamAnalysisList);
        // 获取总数
        Integer count = pageParamAnalysisService.count(pageParamAnalysis);
        result.setTotal(count);
        return result;
    }

    /**
     * 获取地市
     *
     * @param pageParamAnalysis
     * @return
     */
    @RequestMapping(value = "listCity")
    public AntdProResult listCity(PageParamAnalysis pageParamAnalysis) {
        AntdProResult result = new AntdProResult();
        List<PageParamAnalysis> pageParamAnalysisList = pageParamAnalysisService.listCity(pageParamAnalysis);
        result.success(pageParamAnalysisList);
        return result;
    }

    /**
     * 获取子节点
     *
     * @param pageParamAnalysis
     * @return
     */
    @RequestMapping(value = "listChildren")
    public AntdProResult listChildren(PageParamAnalysis pageParamAnalysis) {
        AntdProResult result = new AntdProResult();
        List<PageParamAnalysis> pageParamAnalysisList = pageParamAnalysisService.listChildren(pageParamAnalysis);
        for (PageParamAnalysis statistics : pageParamAnalysisList) {
            statistics.setIsChild("1");
        }
        result.success(pageParamAnalysisList);
        return result;
    }

    /**
     * 获取子节点地市
     *
     * @param pageParamAnalysis
     * @return
     */
    @RequestMapping(value = "listChildCity")
    public AntdProResult listChildCity(@RequestBody PageParamAnalysis pageParamAnalysis) {
        AntdProResult result = new AntdProResult();
        List<PageParamAnalysis> pageParamAnalysisList = pageParamAnalysisService.listChildCity(pageParamAnalysis);
        result.success(pageParamAnalysisList);
        return result;
    }

    /**
     * 获取访前
     *
     * @param pageParamAnalysis
     * @return
     */
    @RequestMapping(value = "listVisitBefore")
    public AntdProResult listVisitBefore(PageParamAnalysis pageParamAnalysis) {
        AntdProResult result = new AntdProResult();
        List<PageParamAnalysis> pageParamAnalysisList = pageParamAnalysisService.listVisitBefore(pageParamAnalysis);
        for (PageParamAnalysis statistics : pageParamAnalysisList) {
            statistics.setIsChild("1");
            statistics.setParentUrl(pageParamAnalysis.getUrl());
        }
        result.success(pageParamAnalysisList);
        return result;
    }

    /**
     * 获取访前地市
     *
     * @param pageParamAnalysis
     * @return
     */
    @RequestMapping(value = "listVisitBeforeCity")
    public AntdProResult listVisitBeforeCity(@RequestBody PageParamAnalysis pageParamAnalysis) {
        AntdProResult result = new AntdProResult();
        List<PageParamAnalysis> pageParamAnalysisList = pageParamAnalysisService.listVisitBeforeCity(pageParamAnalysis);
        result.success(pageParamAnalysisList);
        return result;
    }

}
